package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcFollowDataBoxygenMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcFollowDataBoxygen;
import com.csun.cmny.provider.model.dto.follow.FollowDataPageQuery;
import com.csun.cmny.provider.model.dto.follow.UpFollowDataBoxygenDto;
import com.csun.cmny.provider.model.enums.follow.FollowBoxygenStatusEnum;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcFollowDataBoxygenService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-16 21:39
 * @Description: the class dmc follow data boxygen service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcFollowDataBoxygenServiceImpl extends BaseService<DmcFollowDataBoxygen>
        implements DmcFollowDataBoxygenService {

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcFollowDataBoxygenMapper followDataBoxygenDao;

    /**
     * query dmc follow data boxygen list with page
     *
     * @param followDataPageQuery follow data query page
     *
     * @return the page info
     */
    @Override
    public PageInfo queryDmcFollowDataBoxygenListWithPage(FollowDataPageQuery followDataPageQuery) {

        PageHelper.startPage(followDataPageQuery.getPageNum(), followDataPageQuery.getPageSize());

        List<DmcFollowDataBoxygen> dmcFollowDataBoxygenList = followDataBoxygenDao
                .queryDmcFollowDataBoxygenListWithPage(followDataPageQuery);

        return new PageInfo<>(dmcFollowDataBoxygenList);
    }

    /**
     * up follow data boxygen
     *
     * @param upFollowDataBoxygenDto up follow data boxygen dto
     * @param loginAuthDto           login auth dto
     * @return the int.
     */
    @Override
    public Integer upFollowDateBoxygen(UpFollowDataBoxygenDto upFollowDataBoxygenDto, LoginAuthDto loginAuthDto) {

        // 1.校验设备
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsFollowByDeviceId(upFollowDataBoxygenDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610041, upFollowDataBoxygenDto.getDeviceId());
        }

        // 2.校验参数
        Preconditions.checkArgument(!PubUtils.isNull(upFollowDataBoxygenDto.getBoxygen()),
                cmnyMessageSource.getMessage("follow.up.boxygen.null", null));

        // 3.换算状态
        String status = FollowBoxygenStatusEnum.getStatus(upFollowDataBoxygenDto.getBoxygen());
        if (PubUtils.isNull(status)) {
            status = cmnyMessageSource.getMessage("follow.boxygen.status.error", null);
        }

        // 4.插入数据
        DmcFollowDataBoxygen dmcFollowDataBoxygen = new DmcFollowDataBoxygen();
        BeanUtils.copyProperties(upFollowDataBoxygenDto, dmcFollowDataBoxygen);
        dmcFollowDataBoxygen.setStatus(status);
        dmcFollowDataBoxygen.setUpdateInfo(loginAuthDto);

        return followDataBoxygenDao.insertSelective(dmcFollowDataBoxygen);
    }

    @Override
    public Map<String, Object> getRecentData(Integer oldmanId) {
        return followDataBoxygenDao.getRecentData(oldmanId);
    }
}
